package com.li.manage.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.li.commons.util.ImageUtils;
import com.li.commons.util.StringUtils;
import com.li.manage.enums.UserStateEnum;
import com.li.manage.model.User;
import com.li.manage.query.UserQuery;
import com.li.manage.service.UserService;

/**
 * 用户
 * @author liwm
 *
 */
@Controller
@RequestMapping("/user")
public class UserAction extends BaseAction{

	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private UserService userService;
	
	/**
	 * 列表
	 * /user/userList
	 * @param req
	 * @return
	 */
	@RequestMapping("/userList")
	public ModelAndView userList(HttpServletRequest req,UserQuery userQuery){
		ModelAndView mav = new ModelAndView();
		System.out.println("--------------我是改变的");
		List<User> userList = userService.getUserList(userQuery);
		mav.addObject("userList", userList);
		mav.setViewName("/user/userList");
		System.out.println("--------------我是改变的");
		return mav;
	}
	/**
	 * 跳转
	 * @param req
	 * @param code
	 * @return
	 */
	@RequestMapping("/toUserUpdate")
	public ModelAndView toUserUpdate(HttpServletRequest req,String code){
		ModelAndView mav = new ModelAndView();
		String flag = null;
		if(StringUtils.isEmpty(code)){
			flag = "add";
		}else{
			flag = "update";
			User user = userService.getUserByCode(code);
			mav.addObject("user", user);
		}
		mav.addObject("flag", flag);
		mav.addObject("DOMAIN_NAME", DOMAIN_NAME);
		mav.setViewName("/user/userUpdate");
		return mav;
	}
	/**
	 * 添加或修改
	 * @param req
	 * @param user
	 * @param flag
	 * @param headerPicture
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/userUpdate")
	public ModelAndView userUpdate(HttpServletRequest req,User user,String flag,
			@RequestParam(value="headerPicture",required=false) CommonsMultipartFile headerPicture) throws IOException{
		ModelAndView mav = new ModelAndView();
		if(flag!=null){
			if("add".equals(flag)){
				String maxCode = userService.getMaxCode();
				Integer code = null;
				if(maxCode!=null){
					code = Integer.parseInt(maxCode)+1;
				}else{
					code = 1000001;
				}
				String imgPath = IMG_USER_HEADER_DIR+ImageUtils.getUserHeader(code+"");
				if(headerPicture!=null&&headerPicture.getSize()>0){
					uploadImg(imgPath, headerPicture);
				}
				user.setHeaderPic(imgPath);
				user.setCode(code+"");
				user.setCreateTime(new Date());
				user.setPassword(passwordEncoder.encode("123456"));
				user.setState(UserStateEnum.common.getCode());
				userService.addUser(user);
			}else{
				if(headerPicture!=null&&headerPicture.getSize()>0){
					if(user.getHeaderPic()!=null){
						deleteImg(user.getHeaderPic());
					}else{
						String imgPath = IMG_USER_HEADER_DIR+ImageUtils.getUserHeader(user.getCode());
						user.setHeaderPic(imgPath);
					}
					uploadImg(user.getHeaderPic(), headerPicture);
				}
				userService.updateByCode(user);
			}
		}
		mav.setViewName("redirect:/user/userList");
		return mav;
	}
	/**
	 * 删除
	 * @param req
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteUser")
	public Map<String,String> deleteUser(HttpServletRequest req,String code){
		Map<String,String> map = new HashMap<String,String>();
		if(!StringUtils.isEmpty(code)){
			userService.deleteUserByCode(code);
			map.put("result", "删除成功！");
		}else{
			map.put("result", "参数不能为空！");
		}
		return map;
	}
}
